//
//  VisitorLoginView.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/16.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit
import SnapKit

///定义协议 ，点击登陆按钮是 控制器modal出来一个视图
@objc protocol VisitorLoginViewDelegate:NSObjectProtocol {
    //声明协议方法
    func userWillLogin()
    
    optional func userWillRegister()
}

class VisitorLoginView: UIView {
    
    //设置代理属性 //声明代理对象  注意 在声明代理对象一定要使用 weak 防止循环引用
    weak var loginViewDelegate:VisitorLoginViewDelegate?
 
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    ///添加控件到视图上
    func setUpUI(){
        addSubview(circleicon)
        addSubview(backImage)
        addSubview(largehouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        ///使用第三方框架，设置控件位置
        
        //遮挡背景
        backImage.snp_updateConstraints { (make) -> Void in
            make.center.equalTo(circleicon.snp_center)
        }
        
        //设置视图背景颜色 与遮挡颜色一样
        let value = CGFloat(236.0/255.0)
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //圆圈
        circleicon.snp_updateConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
            //房子
            largehouse.snp_updateConstraints(closure: { (make) -> Void in
                make.center.equalTo(circleicon.snp_center)
            })
            
            //文字
            tipLabel.snp_updateConstraints(closure: { (make) -> Void in
                make.centerX.equalTo(circleicon.snp_centerX)
                make.top.equalTo(circleicon.snp_bottom).offset(16)
                //固定宽度 高度
                make.width.equalTo(230)
                make.height.equalTo(40)
            })
            
            //登陆按钮
            loginBtn.snp_updateConstraints(closure: { (make) -> Void in
                make.left.equalTo(tipLabel.snp_left)
                make.width.equalTo(100)
                make.top.equalTo(tipLabel.snp_bottom).offset(16)
            })
            //注册按钮
            //按钮
            registerBtn.snp_updateConstraints(closure: { (make) -> Void in
                make.right.equalTo(tipLabel.snp_right)
                make.width.equalTo(100)
                make.top.equalTo(tipLabel.snp_bottom).offset(16)
            })
            
        }
        //添加按钮点击事件
        loginBtn.addTarget(self, action: "LoginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "RegisterBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    //点击事件方法
    @objc private func LoginBtnDidClick() {
        loginViewDelegate?.userWillLogin()
    }
    
    @objc private func RegisterBtnDidClick() {
        loginViewDelegate?.userWillRegister?()
    }
    
    //设置页面信息
    func setUpUIWithInfo(tipText:String,circleiconName:String?){
        tipLabel.text = tipText
        if circleiconName != nil {
            //不在首页
            circleicon.image = UIImage(named: circleiconName!)
            largehouse.hidden = true
            //把图片至最前
            self.bringSubviewToFront(circleicon)
            
        }else{
            //一定在首页
            //开始动画
            startAnimation()
        }
        
    }
    
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //旋转到
        anim.toValue = 2 * M_PI
        //旋转事件
        anim.duration = 15
        //重复次数
        anim.repeatCount = MAXFLOAT
        
        //动画处于非活跃状态下 时 动画继续
        anim.removedOnCompletion = false
        
        //添加到 圆圈图层上
        circleicon.layer.addAnimation(anim, forKey: nil)
    
    }
    
    
    
    
    //懒加载子控件
    lazy var circleicon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var largehouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var tipLabel:UILabel = UILabel(title: "关注一些人，会这里看看有设么惊喜", textSize: 14, textColor: UIColor.lightGrayColor())

    
    
    lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState:.Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
        }()
    
    lazy var registerBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState:.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
        }()
    //遮盖的灰色图片
    lazy var backImage :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
